package org.springframework.web.servlet.mvc.method.annotation;

import org.springframework.cache.support.NullValue;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyAdviceChain;

import java.util.List;

public class SimpleRequestResponseBodyAdviceChain extends RequestResponseBodyAdviceChain {
    /**
     * Create an instance from a list of objects that are either of type
     * {@code ControllerAdviceBean} or {@code RequestBodyAdvice}.
     *
     * @param requestResponseBodyAdvice
     */
    public SimpleRequestResponseBodyAdviceChain(List<Object> requestResponseBodyAdvice) {
        super(requestResponseBodyAdvice);
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType contentType,
                                  Class<? extends HttpMessageConverter<?>> converterType,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        Object object = super.beforeBodyWrite(body, returnType, contentType, converterType, request, response);

        return object == null ? NullValue.INSTANCE : object;
    }

}
